package modified;
import java.awt.Color;

public class Board {
  private SimpleBoard simpleBoard;
  private final int defaultSize = 8;
  private Player playerOne;
  private Player playerTwo;

  // Static piece identifiers that cleints expect.
  public static int SINGLE = 0;
  public static int KING = 1;

  public Board(Player playerOne, Player playerTwo) {
    simpleBoard = new SimpleBoard(defaultSize);
    this.placeCheckers();
    this.playerOne = playerOne;
    this.playerTwo = playerTwo;
  }

  public boolean occupied(int legacyPieceNumber) {
    Space spaceAtNumber = spaceAtLegacyPieceNumber(legacyPieceNumber);
    return spaceAtNumber.hasChecker();
  }

  public void kingPiece(int legacyPieceNumber) {
    Checker checkerAtSpace = checkerAtLegacyPieceNumber(legacyPieceNumber);
    checkerAtSpace.makeKing();
  }

  public void removePiece(int legacyPieceNumber) {
    Space spaceAtNumber = spaceAtLegacyPieceNumber(legacyPieceNumber);
    spaceAtNumber.removeChecker();
  }

  public int sizeOf() {
    return simpleBoard.getSize() * simpleBoard.getSize();
  }

  public Piece getPieceAt(int legacyPieceNumber) {
    Piece returnedPiece;
    Checker checkerAtSpace = checkerAtLegacyPieceNumber(legacyPieceNumber);

    if(checkerAtSpace.isKing()) {
      returnedPiece = new KingPiece(checkerAtSpace);
    }
    else {
      returnedPiece = new SinglePiece(checkerAtSpace);
    }

    return returnedPiece;
  }

  public Color colorAt(int legacyPieceNumber) {
    Space spaceToCheck = spaceAtLegacyPieceNumber(legacyPieceNumber);
    if(!spaceToCheck.hasChecker()) {
      return null;
    }
    else {
      Color returnedColor;
      Checker checkerAtSpace = spaceToCheck.getChecker();

      if(checkerAtSpace.getColor() == CheckerColor.WHITE) {
        returnedColor = Color.white;
      }
      else {
        returnedColor = Color.blue;
      }

      return returnedColor;
    }
  }

  public int movePiece(int legacyFrom, int legacyTo) {
    Space spaceFrom = spaceAtLegacyPieceNumber(legacyFrom);
    Space spaceTo = spaceAtLegacyPieceNumber(legacyTo);
    simpleBoard.moveChecker(spaceFrom, spaceTo);

    int returnCode;
    if(this.occupied(legacyTo)) {
      returnCode = -1;
    }
    else {
      returnCode = 0;
    }

    return returnCode;
  }

  // This is not a great way of doing it, but efficiency is not the point here.
  public boolean hasPieceOf(Color color) {
    for (int row = 1; row <= simpleBoard.getSize(); row++) {
      for (int col = 1; col <= simpleBoard.getSize(); col++) {
        Space currentSpace = simpleBoard.getSpace(row, col);
        Checker currentChecker = currentSpace.getChecker();
        CheckerColor currentCheckerColor = currentChecker.getColor();

        if(color == Color.white && currentCheckerColor == CheckerColor.WHITE) {
          return true;
        }
        else if (color == Color.blue && currentCheckerColor == CheckerColor.BLACK) {
          return true;
        }
      }
    }

    // No matches.
    return false;
  }

  private Space spaceAtLegacyPieceNumber(int legacyPieceNumber) {
    int simpleBoardRow = 1 + (int) Math.floor((legacyPieceNumber - 1) / simpleBoard.getSize());
    int simpleBoardCol = 1 + (legacyPieceNumber - 1) % simpleBoard.getSize();
    return simpleBoard.getSpace(simpleBoardRow, simpleBoardCol);
  }

  private Checker checkerAtLegacyPieceNumber(int legacyPieceNumber) {
    Space spaceAtNumber = spaceAtLegacyPieceNumber(legacyPieceNumber);
    return spaceAtNumber.getChecker();
  }

  private void placeCheckers() {
    // Player 1.
    for(int legacyPieceNumber = 1; legacyPieceNumber <= 23; legacyPieceNumber += 2) {
      // Handle alternating rows.
      if(legacyPieceNumber == 9) {
        legacyPieceNumber--;
      }
      if(legacyPieceNumber == 16) {
        legacyPieceNumber++;
      }

      placeSingleChecker(legacyPieceNumber, playerOne, CheckerColor.BLACK);
    }

    // Player 2.
    for(int legacyPieceNumber = 40; legacyPieceNumber <= 62; legacyPieceNumber += 2) {
      // Handle alternating rows.
      if(legacyPieceNumber == 48) {
        legacyPieceNumber++;
      }
      if(legacyPieceNumber == 57) {
        legacyPieceNumber--;
      }

      placeSingleChecker(legacyPieceNumber, playerTwo, CheckerColor.WHITE);
    }
  }

  private void placeSingleChecker(int legacyPieceNumber, Player player, CheckerColor color) {
    Space spaceToHoldChecker = this.spaceAtLegacyPieceNumber(legacyPieceNumber);
    Checker checkerToPlace = new Checker(false, player, color);
    spaceToHoldChecker.placeChecker(checkerToPlace);
  }
}
